package com.cqupt.controller;


import com.cqupt.model.pojo.Article;
import com.cqupt.model.pojo.ArticleCategory;
import com.cqupt.model.pojo.UserArticle;
import com.cqupt.model.vo.*;
import com.cqupt.service.ArticleCategoryService;
import com.cqupt.service.ArticleService;
import com.cqupt.service.UserArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/article")
@Tag(name = "文章阅读模块")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @Resource
    private UserArticleService userArticleService;

    @Resource
    private ArticleCategoryService articleCategoryService;

    @GetMapping("/articleCategory")
    @Operation(summary = "获取文章全部分类")
    public Result articleCategory() {
        List<ArticleCategory> list = articleCategoryService.list();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/list")
    @Operation(summary = "获取所有文章")
    public Result list() {
        List<Article> list = articleService.list();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/category")
    @Operation(summary = "根据分类id获取文章")
    public Result category(@RequestParam int categoryId) {
        List<Article> list = articleService.category(categoryId, null, null).getRecords();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/getOne")
    @Operation(summary = "获取某一篇文章")
    public Result getOne(@RequestParam("id") int id) {
        ArticleVO one = articleService.getArticle(id);

        return Result.build(one, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/mark")
    @Operation(summary = "收藏文章")
    public Result mark(@RequestParam("id") int id) {
        userArticleService.mark(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/unmark")
    @Operation(summary = "取消收藏文章")
    public Result unmark(@RequestParam("id") int id) {
        userArticleService.unmark(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/allMarked")
    @Operation(summary = "获取全部收藏的文章")
    public Result getAllMarked() {
        List<Article> list = userArticleService.getAllMarked();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/getMarked")
    @Operation(summary = "根据文章分类id获取收藏的文章")
    public Result getMarkedByCategoryId(@RequestParam int categoryId) {
        List<Article> list = userArticleService.getMarkedByCategoryId(categoryId);
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/audio")
    @Operation(summary = "根据文章id获取AI阅读")
    public Result audio(@RequestParam int id) {
        String URL = "http://127.0.0.1:9000/app-bucket/audio/" + id + ".mp3";
        return Result.build(URL, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/audio2")
    @Operation(summary = "获取每一段的AI阅读")
    public Result audio2(@RequestParam int id, @RequestParam int num) {
        String URL = "http://127.0.0.1:9000/app-bucket/audio2/" + id + "." + num + ".mp3";
        return Result.build(URL, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/follow")
    @Operation(summary = "文章跟读并打分")
    public Result follow(@RequestParam String text, @RequestParam MultipartFile file) {
        ScoreVO scoreVO = articleService.follow(text, file);
        return Result.build(scoreVO, ResultCodeEnum.SUCCESS);
    }

}
